﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Core;
using System.Xml.Linq;

namespace xManagerWebApp.__AdminCompany
{
    public partial class WUC_ViewExpenseChain : System.Web.UI.UserControl
    {
        private PlaceHolder[] placeHolders;

        public Guid? ChainID
        {
            get
            {
                if (ViewState["ChainID"] != null)
                    return (Guid?)ViewState["ChainID"];
                else
                    return null;
            }
            set { ViewState["ChainID"] = value; }
        }

        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);

            if (ChainID.HasValue && ChainID.Value.CompareTo(default(Guid)) != 0)
            {
                Chain c;

                using (DataAccessDataContext dc = new DataAccessDataContext())
                {
                    var query = from expensechain in dc.Chains
                                where expensechain.Id.Equals(ChainID.Value)
                                select expensechain;

                    c = query.Single<Chain>();
                }

                this.ChainName.Text = c.Name;

                LinkedList<String>[] tree = GetTreeFromXML(c.Xml);
                placeHolders = new PlaceHolder[tree.Length];

                for (int i = 0; i < tree.Length; ++i)
                {
                    InsertNewPlaceHolder(i + 1);
                    foreach (String s in tree[i])
                        AddChainEntry(s, i + 1);
                }
            }
            else
            {
                this.ChainName.Text = "";
            }

        }

        private void InsertNewPlaceHolder(int i)
        {
            if (i > 0)
            {
                Label l = new Label();
                l.Text = "Level: " + i;

                PlaceHolder p = new PlaceHolder();
                placeHolders[i - 1] = p;

                this.Levels.Controls.Add(l);
                this.Levels.Controls.Add(new LiteralControl("<br />"));
                this.Levels.Controls.Add(p);
                this.Levels.Controls.Add(new LiteralControl("<br />"));
            }
        }


        private void AddChainEntry(String x, int level)
        {
            Label l = new Label();
            l.Text = x;

            placeHolders[level - 1].Controls.Add(l);
            placeHolders[level - 1].Controls.Add(new LiteralControl("<br />"));
        }


        private LinkedList<String>[] GetTreeFromXML(XElement doc)
        {

            int maxlevels = int.Parse(doc.Attribute("maxLevel").Value);
            LinkedList<String>[] tree = new LinkedList<String>[maxlevels];

            int current = -1;
            Guid id;

            using (DataAccessDataContext dc = new DataAccessDataContext())
            {


                foreach (XElement x in doc.Elements())
                {
                    current = int.Parse(x.Attribute("level").Value);
                    id = new Guid(x.Attribute("id").Value);

                    var query = from deplevel in dc.DepartmentLevels
                                from dep in dc.Departments
                                where deplevel.Id.Equals(id)
                                where dep.Id.Equals(deplevel.DepartmentId)
                                select new
                                {
                                    DepLevelNome = deplevel.Name,
                                    DepLevelNivel = deplevel.Level,
                                    DepNome = dep.Name
                                };

                    var d = query.Single();


                    if (tree[current - 1] == null) tree[current - 1] = new LinkedList<String>();
                    tree[current - 1].AddLast(d.DepNome + " : " + d.DepLevelNome + " (Level " + d.DepLevelNivel + ")");
                }
            }

            return tree;
        }
    }

}